package com.liaoye.hellocustomview.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

public class DoubleBufferingSurfaceView extends SurfaceView {

    private Paint mPaint;
    private Path mPath;
    private static final String TAG = DoubleBufferingSurfaceView.class.getSimpleName();
    private List<Integer> list = new ArrayList<>();

    public DoubleBufferingSurfaceView(Context context) {
        super(context);
        init();
    }

    public DoubleBufferingSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DoubleBufferingSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
        mPaint.setColor(Color.RED);
        mPaint.setTextSize(60);
        mPath = new Path();
        getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {
                Log.i(TAG, "surfaceCreated: ");
                drawText(holder);
            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
                Log.i(TAG, "surfaceDestroyed: ");

            }
        });

    }


    private void drawText(SurfaceHolder holder) {
        new Thread(() -> {

            while (true) {
                Rect dirtyRect = new Rect(0, 0, 1, 1);
                Canvas canvas = holder.lockCanvas(dirtyRect);
                Rect canvasRect = canvas.getClipBounds();
                if(getWidth() == canvasRect.width() && getHeight() == canvasRect.height()) {
                    canvas.drawColor(Color.BLACK);
                    holder.unlockCanvasAndPost(canvas);
                } else {
                    holder.unlockCanvasAndPost(canvas);
                    break;
                }
            }

            for (int i = 0; i < 10; i++) {
                int itemWidth = 120;
                int itemHeight = 120;
                Rect rect = new Rect(i * itemWidth, 0, (i + 1) * itemWidth - 10, itemHeight);
                list.add(i);
                Canvas canvas = holder.lockCanvas(rect);
                if (canvas != null) {
                    canvas.drawColor(Color.GREEN);
                    canvas.drawText(i + "", i * itemWidth + 10, itemHeight / 2, mPaint);
                }
                holder.unlockCanvasAndPost(canvas);

                try {
                    Thread.sleep(800);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
